package com.example.tcpTio.client;

import com.alibaba.fastjson.JSONObject;
import com.example.common.TioPacket;
import com.example.tioClient.ClientStarter;
import com.example.tioClient.handler.ClientHandlerBase;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.intf.Packet;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.atomic.AtomicInteger;

public class TioClientStarter {


    /**
     * 启动程序入口
     */
    public static void main(String[] args) throws Exception {

        ClientStarter clientStarter=new ClientStarter("127.0.0.1",10000);

        AtomicInteger num=new AtomicInteger(1);

        clientStarter.setClientListener(new ClientHandlerBase() {
            private TioPacket heartbeatPacket = new TioPacket();
            @Override
            public void handler(Packet packet, ChannelContext channelContext) throws Exception {
                TioPacket helloPacket = (TioPacket) packet;
                byte[] body = helloPacket.getBody();
                if (body != null) {
                    String str = new String(body, TioPacket.CHARSET);
                    System.out.println("收到消息：" + str);
                }
                return;
            }

            @Override
            public Packet heartbeatPacket(ChannelContext channelContext) {
//                JSONObject msg=new JSONObject();
//                JSONObject data=new JSONObject();
//                data.put("clientID","444");
//                data.put("parkNum","10");
//                msg.put("type","HEARTBEAT");
//                msg.put("data",data);

                JSONObject msg=new JSONObject();
                JSONObject data=new JSONObject();
                data.put("sendClientID","444");
                data.put("commanClientID","0a070299963f014f46f06ec54b801576");
                data.put("sendData",String.format("你还发射点发射点发%s",  num.addAndGet(1)));
                msg.put("type","START");
                msg.put("data",data);
                try {
                    heartbeatPacket.setBody(msg.toJSONString().getBytes(TioPacket.CHARSET));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return heartbeatPacket;
            }
        },true);

        clientStarter.start();
//
//        send(1);
//
//        clientStarter.close();


//        ShortClientUtile shortClientUtile=new ShortClientUtile();
//
//        shortClientUtile.start();
//        TioPacket packet = new TioPacket();
//        packet.setBody("123456".getBytes(TioPacket.CHARSET));
//        shortClientUtile.sendToUser(packet);
//        Thread.sleep(2000);
//        packet.setBody("978542".getBytes(TioPacket.CHARSET));
//        shortClientUtile.sendToUser(packet);
//
//        shortClientUtile.close();



    }


    private static void send(int i) throws Exception {
        TioPacket packet = new TioPacket();

        JSONObject msg=new JSONObject();
        JSONObject data=new JSONObject();
        data.put("clientID","333");
        data.put("parkNum",i);
        msg.put("type","HEARTBEAT");
        msg.put("data",data);
        packet.setBody("".getBytes(TioPacket.CHARSET));
        Tio.send(ClientStarter.clientChannelContext, packet);

    }

}
